package com.xique.door.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.api.request.build.RemoteAreaListRequest;
import com.xique.door.bean.domain.BuildArea;
import com.xique.door.bean.domain.BuildAreaV1;
import com.xique.door.bean.domain.BuildBuilding;
import com.xique.door.bean.request.buildArea.BuildAreaAddRequest;
import com.xique.door.bean.request.buildArea.BuildAreaDeleteRequest;
import com.xique.door.bean.request.buildArea.BuildAreaEditRequest;
import com.xique.door.bean.search.build.AreaListSearch;
import com.xique.door.bean.vo.area.AreaListVO;
import com.xique.door.biz.service.IBuildAreaBizService;
import com.xique.door.service.IBuildAreaService;
import com.xique.door.service.IBuildBuildingService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author shd
 * @create 2021/9/10 9:07
 */
@Service
@EnableAsync
public class BuildAreaBizServiceImpl implements IBuildAreaBizService {

    @Autowired
    private IBuildAreaService buildAreaService;

    @Autowired
    private IBuildBuildingService buildBuildingService;


    private AjaxResult checkBuildAreaAddRequest(BuildAreaAddRequest request) {

        if (StringUtils.isEmpty(request.getCode())) {
            return AjaxResult.error("请输入编号");
        }

        if (2 != request.getCode().length()) {
            return AjaxResult.error("请输入正确的编号");
        }

        if (StringUtils.isEmpty(request.getAreaName())) {
            return AjaxResult.error("请输入区域名称");
        }

        // 判断编号是否重复
        BuildArea search = new BuildArea();
        search.setParkId(request.getParkId());
        search.setCode(request.getCode());
        List<BuildArea> list = buildAreaService.selectBuildAreaList(search);
        if (CollectionUtils.isNotEmpty(list)) {
            return AjaxResult.error("编号重复");
        }

        return AjaxResult.success();
    }

    @Override
    public AjaxResult areaEdit(BuildAreaEditRequest request) {
        BuildArea area = OrikaUtil.convert(request, BuildArea.class);
        int count = buildAreaService.updateBuildArea(area);
        if (count <= 0) {
            return AjaxResult.error("编辑失败");
        }
        return AjaxResult.success("编辑成功");
    }


    @Override
    public AjaxResult areaDelete(BuildAreaDeleteRequest request) {
        List<BuildBuilding> buildingList = buildBuildingService.selectBuildBuildingListByAreaIds(Arrays.asList(Convert.toLongArray(request.getIds())));
        if (CollectionUtils.isNotEmpty(buildingList)) {
            return AjaxResult.error("请先删除该区域下的楼栋");
        }
        int count = buildAreaService.deleteBuildAreaByIds(request.getIds());

        if (count <= 0) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }


    @Override
    public AjaxResult areaAdd(BuildAreaAddRequest request) {
        // 可行性验证
        AjaxResult result = checkBuildAreaAddRequest(request);
        if (AjaxResult.isError(result)) {
            return result;
        }

        // 1.写入到区域表
        BuildArea area = OrikaUtil.convert(request, BuildArea.class);
        int count = buildAreaService.insertBuildArea(area);
        if (count <= 0) {
            return AjaxResult.error("添加失败");
        }

        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult selectAreaList(RemoteAreaListRequest request) {

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", new ArrayList<>());
        map.put("total", 0);

        AreaListSearch search = OrikaUtil.convert(request, AreaListSearch.class);
        search.setParkId(request.getProjectId());
        IPage<BuildAreaV1> page = buildAreaService.selectAreaPageV1(search);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return AjaxResult.success(map);
        }

        List<AreaListVO> vos = OrikaUtil.converts(page.getRecords(), AreaListVO.class);

        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }
}
